using MicroMsg.Manager;
using MicroMsg.Plugin.Sns.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class AlbumImgControl : SnsHttpImgCtrlBase
	{
		public delegate void SnsImageClick(object sender, object object_0);
		private List<TloMedia> mediaList;
		private List<Image> imageList;
		private string[] strUrlList = new string[4];
		private SnsGroupList mGroupList;
		private Grid descGrid;
		private Grid sendErrorGrid;
		private SmileyText eText;
		private Image sendErrorImg;
		private AlbumPrivateCtrl privateCtrl;
		internal Canvas LayoutRoot;
		private bool _contentLoaded;
		public event AlbumImgControl.SnsImageClick ImageClick;
		public AlbumImgControl()
		{
			this.InitializeComponent();
			this.LayoutRoot.set_Background(R.theme.SNS_USERPAGEITEM_TAKEPHOTO_BK_BRUSH);
			base.add_Loaded(new RoutedEventHandler(this.AlbumImgControl_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.AlbumImgControl_Unloaded));
		}
		private void AlbumImgControl_Unloaded(object sender, RoutedEventArgs e)
		{
			this.LayoutRoot.remove_Tap(new EventHandler<GestureEventArgs>(this.LayoutRoot_Tap));
		}
		private void AlbumImgControl_Loaded(object sender, RoutedEventArgs e)
		{
			this.LayoutRoot.add_Tap(new EventHandler<GestureEventArgs>(this.LayoutRoot_Tap));
		}
		protected override void ObjectCome(object object_0)
		{
			if (object_0 != null && object_0 is snsMediaDownloadInfo)
			{
				snsMediaDownloadInfo snsMediaDownloadInfo = object_0 as snsMediaDownloadInfo;
				if (snsMediaDownloadInfo.object_0 != null && snsMediaDownloadInfo.object_0 is BitmapImage)
				{
					for (int i = 0; i < this.imageList.get_Count(); i++)
					{
						if (this.strUrlList[i] == snsMediaDownloadInfo.string_0)
						{
							this.imageList.get_Item(i).set_Source(snsMediaDownloadInfo.object_0 as BitmapImage);
						}
					}
				}
				return;
			}
		}
		private void GenImageList(int count)
		{
			int num = Math.Min(4, count);
			if (this.imageList == null)
			{
				this.imageList = new List<Image>();
			}
			if (this.imageList.get_Count() < num)
			{
				for (int i = this.imageList.get_Count(); i < num; i++)
				{
					Image image = new Image();
					image.set_Stretch(3);
					image.set_HorizontalAlignment(1);
					image.set_VerticalAlignment(1);
					Image image2 = image;
					this.imageList.Add(image2);
					if (this.descGrid == null)
					{
						this.LayoutRoot.get_Children().Add(image2);
					}
					else
					{
						this.LayoutRoot.get_Children().Insert(this.LayoutRoot.get_Children().get_Count() - 1, image2);
					}
				}
				return;
			}
			if (this.imageList.get_Count() > num)
			{
				for (int j = this.imageList.get_Count() - 1; j > num - 1; j--)
				{
					this.LayoutRoot.get_Children().Remove(this.imageList.get_Item(j));
					this.imageList.RemoveAt(j);
				}
			}
		}
		private void UIArrangeImages()
		{
			if (this.imageList == null || this.imageList.get_Count() == 0)
			{
				return;
			}
			if (this.imageList.get_Count() == 1)
			{
				this.imageList.get_Item(0).set_Width(base.get_Width());
				this.imageList.get_Item(0).set_Height(base.get_Height());
				this.imageList.get_Item(0).set_Stretch(3);
				this.imageList.get_Item(0).set_Margin(new Thickness(0.0, 0.0, 0.0, 0.0));
				return;
			}
			if (this.imageList.get_Count() == 2)
			{
				this.imageList.get_Item(0).set_Width(base.get_Width() / 2.0 - 1.0);
				this.imageList.get_Item(0).set_Height(base.get_Height());
				this.imageList.get_Item(0).set_Stretch(3);
				this.imageList.get_Item(0).set_Margin(new Thickness(0.0, 0.0, 0.0, 0.0));
				this.imageList.get_Item(1).set_Width(base.get_Width() / 2.0 - 1.0);
				this.imageList.get_Item(1).set_Height(base.get_Height());
				this.imageList.get_Item(1).set_Stretch(3);
				this.imageList.get_Item(1).set_Margin(new Thickness(base.get_Width() / 2.0 + 1.0, 0.0, 0.0, 0.0));
				return;
			}
			if (this.imageList.get_Count() == 3)
			{
				this.imageList.get_Item(0).set_Width(base.get_Width() / 2.0 - 1.0);
				this.imageList.get_Item(0).set_Height(base.get_Height());
				this.imageList.get_Item(0).set_Stretch(3);
				this.imageList.get_Item(0).set_Margin(new Thickness(0.0, 0.0, 0.0, 0.0));
				this.imageList.get_Item(1).set_Width(base.get_Width() / 2.0 - 1.0);
				this.imageList.get_Item(1).set_Height(base.get_Height() / 2.0 - 1.0);
				this.imageList.get_Item(1).set_Stretch(3);
				this.imageList.get_Item(1).set_Margin(new Thickness(base.get_Width() / 2.0 + 1.0, 0.0, 0.0, 0.0));
				this.imageList.get_Item(2).set_Width(base.get_Width() / 2.0 - 1.0);
				this.imageList.get_Item(2).set_Height(base.get_Height() / 2.0 - 1.0);
				this.imageList.get_Item(2).set_Stretch(3);
				this.imageList.get_Item(2).set_Margin(new Thickness(base.get_Width() / 2.0 + 1.0, base.get_Height() / 2.0 + 1.0, 0.0, 0.0));
				return;
			}
			this.imageList.get_Item(0).set_Width(base.get_Width() / 2.0 - 1.0);
			this.imageList.get_Item(0).set_Height(base.get_Height() / 2.0 - 1.0);
			this.imageList.get_Item(0).set_Stretch(3);
			this.imageList.get_Item(0).set_Margin(new Thickness(0.0, 0.0, 0.0, 0.0));
			this.imageList.get_Item(1).set_Width(base.get_Width() / 2.0 - 1.0);
			this.imageList.get_Item(1).set_Height(base.get_Height() / 2.0 - 1.0);
			this.imageList.get_Item(1).set_Stretch(3);
			this.imageList.get_Item(1).set_Margin(new Thickness(base.get_Width() / 2.0 + 1.0, 0.0, 0.0, 0.0));
			this.imageList.get_Item(2).set_Width(base.get_Width() / 2.0 - 1.0);
			this.imageList.get_Item(2).set_Height(base.get_Height() / 2.0 - 1.0);
			this.imageList.get_Item(2).set_Stretch(3);
			this.imageList.get_Item(2).set_Margin(new Thickness(0.0, base.get_Height() / 2.0 + 1.0, 0.0, 0.0));
			this.imageList.get_Item(3).set_Width(base.get_Width() / 2.0 - 1.0);
			this.imageList.get_Item(3).set_Height(base.get_Height() / 2.0 - 1.0);
			this.imageList.get_Item(3).set_Stretch(3);
			this.imageList.get_Item(3).set_Margin(new Thickness(base.get_Width() / 2.0 + 1.0, base.get_Height() / 2.0 + 1.0, 0.0, 0.0));
		}
		public void LoadSnsInfoImage(TimeLineObject tlObj, SnsGroupList GroupList)
		{
			this.mediaList = tlObj.content.mediaList;
			this.mGroupList = GroupList;
			this.GenImageList(this.mediaList.get_Count());
			this.UIArrangeImages();
			this.CheckShowPrivateCtrl(tlObj);
			this.LoadImage();
		}
		private void CheckShowPrivateCtrl(TimeLineObject tlObj)
		{
			string text = tlObj.strUsername;
			BasePage rootPage = ToolFunction.GetRootPage();
			if (rootPage is SnsUserMainPage)
			{
				SnsUserMainPage snsUserMainPage = rootPage as SnsUserMainPage;
				text = snsUserMainPage.contact.strUsrName;
			}
			if (tlObj.nPrivate == 1 || tlObj.strUsername != text)
			{
				if (this.privateCtrl == null)
				{
					this.privateCtrl = new AlbumPrivateCtrl();
					this.privateCtrl.set_Margin(new Thickness(-32.0, 0.0, 0.0, 0.0));
					this.LayoutRoot.get_Children().Add(this.privateCtrl);
				}
				this.privateCtrl.SetImgKind(AlbumPrivateCtrl.CtrlKind.Kind_Private);
				return;
			}
			if (tlObj.strUsername == AccountMgr.strUsrName && SnsGroupMgr.IsSetCustomGroup(this.mGroupList))
			{
				if (this.privateCtrl == null)
				{
					this.privateCtrl = new AlbumPrivateCtrl();
					this.privateCtrl.set_Margin(new Thickness(-32.0, 0.0, 0.0, 0.0));
					this.LayoutRoot.get_Children().Add(this.privateCtrl);
				}
				this.privateCtrl.SetImgKind(AlbumPrivateCtrl.CtrlKind.Kind_VisiRange);
				return;
			}
			if (this.privateCtrl != null)
			{
				this.LayoutRoot.get_Children().Remove(this.privateCtrl);
				this.privateCtrl = null;
			}
		}
		public void LoadImage()
		{
			for (int i = 0; i < this.imageList.get_Count(); i++)
			{
				this.strUrlList[i] = this.mediaList.get_Item(i).strThumb;
				snsMediaDownloadInfo snsMediaDownloadInfo = new snsMediaDownloadInfo
				{
					string_0 = this.mediaList.get_Item(i).strThumb,
					urlType = (SnsUrlType)this.mediaList.get_Item(i).nUrlType,
					snsObjID = this.mediaList.get_Item(i).nID,
					priority = SnsPriority.LOW
				};
				base.StartRequest(snsMediaDownloadInfo, true);
				if (snsMediaDownloadInfo.object_0 != null && snsMediaDownloadInfo.object_0 is BitmapImage)
				{
					this.imageList.get_Item(i).set_Source(snsMediaDownloadInfo.object_0 as BitmapImage);
				}
				else
				{
					this.imageList.get_Item(i).set_Source(null);
				}
			}
		}
		public void LoadTakePhotoIcon()
		{
			this.GenImageList(1);
			this.imageList.get_Item(0).set_Stretch(0);
			this.imageList.get_Item(0).set_Width(112.0);
			this.imageList.get_Item(0).set_Height(87.0);
			this.imageList.get_Item(0).set_VerticalAlignment(1);
			this.imageList.get_Item(0).set_HorizontalAlignment(1);
			this.imageList.get_Item(0).set_Margin(new Thickness(44.0, 56.0, 0.0, 0.0));
			this.imageList.get_Item(0).set_Source(new ImageSourceConverter().ConvertFromString(R.theme.sns_userpage_takephoto_icon) as ImageSource);
			if (this.privateCtrl != null)
			{
				this.LayoutRoot.get_Children().Remove(this.privateCtrl);
				this.privateCtrl = null;
			}
		}
		private void ClearImgCache()
		{
			if (this.imageList != null && this.imageList.get_Count() != 0)
			{
				for (int i = 0; i < this.imageList.get_Count(); i++)
				{
					this.imageList.get_Item(i).set_Source(null);
				}
				return;
			}
		}
		public void SetDescContent(string strObjDesc)
		{
			if (this.descGrid == null)
			{
				this.descGrid = new Grid();
				this.descGrid.set_Height(34.0);
				this.descGrid.set_Width(base.get_Width());
				this.descGrid.set_Margin(new Thickness(0.0, base.get_Height() - this.descGrid.get_Height(), 0.0, 0.0));
				this.descGrid.set_Background(new SolidColorBrush(Color.FromArgb(200, 0, 0, 0)));
				this.LayoutRoot.get_Children().Add(this.descGrid);
				this.eText = new SmileyText();
				this.eText.set_HorizontalAlignment(2);
				this.eText.set_Margin(new Thickness(10.0, 0.0, 10.0, 0.0));
				this.eText.Foreground = R.theme.SNS_USERPAGEITEM_PHOTODESC_BRUSH;
				this.eText.set_Width(180.0);
				this.eText.set_Height(34.0);
				this.descGrid.get_Children().Add(this.eText);
			}
			this.eText.Text = strObjDesc;
		}
		public void ClearDescContent()
		{
			if (this.descGrid != null)
			{
				this.LayoutRoot.get_Children().Remove(this.descGrid);
				this.descGrid = null;
				this.eText = null;
			}
		}
		public void SetErrorIcon()
		{
			if (this.sendErrorGrid == null)
			{
				this.sendErrorGrid = new Grid();
				this.sendErrorGrid.set_Height(44.0);
				this.sendErrorGrid.set_Width(44.0);
				this.sendErrorGrid.set_Margin(new Thickness(base.get_Width() - this.sendErrorGrid.get_Width() + 10.0, base.get_Height() - this.sendErrorGrid.get_Height() + 10.0, 0.0, 0.0));
				this.LayoutRoot.get_Children().Add(this.sendErrorGrid);
				this.sendErrorImg = new Image();
				this.sendErrorImg.set_Stretch(0);
				this.sendErrorImg.set_Source(new ImageSourceConverter().ConvertFromString(R.theme.Sns_ImgPath_SendError_small) as ImageSource);
				this.sendErrorGrid.get_Children().Add(this.sendErrorImg);
			}
		}
		public void ClearErrorIcon()
		{
			if (this.sendErrorGrid != null)
			{
				this.LayoutRoot.get_Children().Remove(this.sendErrorGrid);
				this.sendErrorGrid = null;
				this.sendErrorImg = null;
			}
		}
		private void LayoutRoot_Tap(object sender, GestureEventArgs e)
		{
			if (this.ImageClick != null)
			{
				this.ImageClick(this, e);
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Album/UI/AlbumImgControl.xaml", 2));
			this.LayoutRoot = (Canvas)base.FindName("LayoutRoot");
		}
	}
}
